// driver.h
// 25 September 2011
// Purpose: Driver for our bit-mouse program

#include "map.h"
#include "mapGen.h"
#include "mouse.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
using std::cout;
using std::endl;
using std::rand;

MapGen::MapGen()
{
	this->initialize(32, 32);
	this->trunkGen(6);
	this->branchGen(10,1000);
	//this->displaymap();
}

MapGen::MapGen(int x,int y)
{
	this->initialize(x, y);
	this->trunkGen(5);
	this->branchGen(10,100);
	//this->displaymap();
}
MapGen::MapGen(int x,int y,int seed)
{
	this->initialize(x, y);
	this->trunkGen(seed);
	this->branchGen(10,100);
	//this->displaymap();
}
MapGen::MapGen(int x,int y,int seed,int odds)
{
	this->initialize(x, y);
	this->trunkGen(seed);
	this->branchGen(odds,100);
	//this->displaymap();
}
MapGen::MapGen(int x,int y,int seed,int odds,int loops)
{
	this->initialize(x, y);
	this->trunkGen2(seed,odds,loops);
	this->displaymap();
	this->branchGen(odds,loops);
	//this->displaymap();
}
MapGen::MapGen(int x,int y,int seed,int odds,int loops,int trunkodds)
{
	this->initialize(x, y);
	this->trunkGen2(seed,trunkodds,loops);
	this->displaymap();
	this->branchGen(odds,loops);
	//this->displaymap();
}

void MapGen::trunkGen(int seed)
{
	std::srand ( seed ); // may make a seed option   time(NULL) 
	int startx = 0;
	int endx = 31;
	int starty=0;
	int endy = 0;
	
	int x=startx;
	int y=starty;
	int xx=0;	//temp x and y
	int yy=0;

	//set start and endpoint to open
	_map[starty] [startx] = 0;

	bool pathset = true;
	while(x<(_w-1))
	{
		pathset = false;
		//start gen
		int c=0;
		this->setaround(x,y);
		if(n==3)
			c++;
		if(w==3)
			c++;
		if(s==3)
			c++;
		if(e==3)
			c++;

		if(n==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %2 ;//0 or 1
				_map[y-1][x]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1 )
					{
						if(y-2>=0 && _map[y-2][x]==3)
						{
							pathset=true;
							yy=y-1;
							xx=x;
						}
						else
							c--;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y-1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y-1;
					xx=x;
				}
			}
		}



		if(w==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %2 ;//0 or 1
				_map[y][x-1]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x-1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x-1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x-1;
				}
			}
		}



		if(s==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %2 ;//0 or 1
				_map[y+1][x]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y+1;
						xx=x;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y+1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y+1;
					xx=x;
				}
			}
		}

		if(e==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %2 ;//0 or 1
				_map[y][x+1]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0 if jump hasent been taken
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x+1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x+1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x+1;
				}
			}
		}

		this->setaround(x,y);

		//no diagnal crap
		if(n==0 && w==0)
			_map[y-1][x-1]=1;
		if(n==0 && e==0)
			_map[y-1][x+1]=1;
		if(s==0 && w==0)
			_map[y+1][x-1]=1;
		if(s==0 && e==0)
			_map[y+1][x+1]=1;
	

		x=xx;	//no longer temp
		y=yy;


		if(pathset==false)   //if it hit a dead end ie 101 and its at the 0
		{
			int c=0;
			this->setaround(x,y);
			if(n==0)
				c++;
			if(w==0)
				c++;
			if(s==0)
				c++;
			if(e==0)
				c++;

			if(n==0)	//if dircetion is valid
			{
				if(c>1)	// if you dont have to go this way
				{			//there is a chance to set this as the path
					{
						int number = rand() %2 ;//0 or 1 again
						if(number==1 )
						{
							if(y-2>=0 && _map[y-2][x]==3)
							{
								pathset=true;
								yy=y-1;
								xx=x;
							}
							else
								c--;
						}
						else
							c--;
					}
				}
				else
				{// this has to be open for there to be an exit
					if(pathset==false)
					{
						pathset=true;
						yy=y-1;
						xx=x;
					}
				}
			}



			if(w==0)	//if dircetion is valid
			{
				if(c>1)	// if you dont have to go this way
				{
					{
						int number = rand() %2 ;//0 or 1 again
						if(number==1)
						{
							pathset=true;
							yy=y;
							xx=x-1;
						}
						else
							c--;
					}
				}
				else
				{// this has to be open for there to be an exit
					if(pathset==false)
					{
						pathset=true;
						yy=y;
						xx=x-1;
					}
				}
			}



			if(s==0)	//if dircetion is valid
			{
				if(c>1)	// if you dont have to go this way
				{		//there is a chance to set this as the path
					{
						int number = rand() %2 ;//0 or 1 again
						if(number==1)
						{
							pathset=true;
							yy=y+1;
							xx=x;
						}
						else
							c--;
					}
				}
				else
				{		// this has to be open for there to be an exit
					if(pathset==false)
					{
						pathset=true;
						yy=y+1;
						xx=x;
					}
				}
			}

			if(e==0)	//if dircetion is valid
			{
				if(c>1)	// if you dont have to go this way
				{	//there is a chance to set this as the path
					{
						int number = rand() %2 ;//0 or 1 again
						if(number==1)
						{
							pathset=true;
							yy=y;
							xx=x+1;
						}
						else
							c--;
					}
				}
				else
				{			// this has to be open for there to be an exit
					if(pathset==false)
					{
						pathset=true;
						yy=y;
						xx=x+1;
					}
				}
			}
		}





		//this->displaymap();
	}
}

void MapGen::trunkGen2(int seed,int odds, int loops)
{
	std::srand ( seed ); // may make a seed option   time(NULL) 
	int startx = 0;
	int endx = 31;
	int starty=0;
	int endy = 0;
	
	int x=startx;
	int y=starty;
	int xx=0;	//temp x and y
	int yy=0;

	//set start and endpoint to open
	_map[starty] [startx] = 0;

	bool pathset = true;
	while(x<(_w-1))
	{
		pathset = false;
		//start gen
		int c=0;
		this->setaround(x,y);
		if(n==3)
			c++;
		if(w==3)
			c++;
		if(s==3)
			c++;
		if(e==3)
			c++;

		if(n==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand()  %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y-1][x]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1 )
					{
						if(y-2>=0 && _map[y-2][x]==3)
						{
							pathset=true;
							yy=y-1;
							xx=x;
						}
						else
							c--;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y-1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y-1;
					xx=x;
				}
			}
		}



		if(w==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y][x-1]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x-1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x-1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x-1;
				}
			}
		}



		if(s==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand()  %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y+1][x]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y+1;
						xx=x;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y+1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y+1;
					xx=x;
				}
			}
		}

		if(e==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand()  %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y][x+1]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0 if jump hasent been taken
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x+1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x+1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x+1;
				}
			}
		}

		this->setaround(x,y);

		//no diagnal crap
		if(n==0 && w==0)
			_map[y-1][x-1]=1;
		if(n==0 && e==0)
			_map[y-1][x+1]=1;
		if(s==0 && w==0)
			_map[y+1][x-1]=1;
		if(s==0 && e==0)
			_map[y+1][x+1]=1;
	


		x=xx;	//no longer temp
		y=yy;

		this->setaround(x,y);

		//no looping
		int loopc=0;
		if(n==0)
			loopc++;
		if(w==0)
			loopc++;
		if(s==0)
			loopc++;
		if(e==0)
			loopc++;
		if(loopc>1)
		{
			int temploop= rand() %loops; //how difficult is is to loop low loop means easy
			if(temploop!=0)
			{
				_map[y][x]=1;
				pathset=false;
			}
		}


		if(pathset==false)   //if it hit a dead end ie 101 and its at the 0
		{
			int c=0;
			this->setaround(x,y);
			if(n==0)
				c++;
			if(w==0)
				c++;
			if(s==0)
				c++;
			if(e==0)
				c++;

			if(n==0)	//if dircetion is valid
			{
				if(c>1)	// if you dont have to go this way
				{			//there is a chance to set this as the path
					{
						int number = rand() %2 ;//0 or 1 again
						if(number==1 )
						{
							if(y-2>=0 && _map[y-2][x]==3)
							{
								pathset=true;
								yy=y-1;
								xx=x;
							}
							else
								c--;
						}
						else
							c--;
					}
				}
				else
				{// this has to be open for there to be an exit
					if(pathset==false)
					{
						pathset=true;
						yy=y-1;
						xx=x;
					}
				}
			}



			if(w==0)	//if dircetion is valid
			{
				if(c>1)	// if you dont have to go this way
				{
					{
						int number = rand() %2 ;//0 or 1 again
						if(number==1)
						{
							pathset=true;
							yy=y;
							xx=x-1;
						}
						else
							c--;
					}
				}
				else
				{// this has to be open for there to be an exit
					if(pathset==false)
					{
						pathset=true;
						yy=y;
						xx=x-1;
					}
				}
			}



			if(s==0)	//if dircetion is valid
			{
				if(c>1)	// if you dont have to go this way
				{		//there is a chance to set this as the path
					{
						int number = rand() %2 ;//0 or 1 again
						if(number==1)
						{
							pathset=true;
							yy=y+1;
							xx=x;
						}
						else
							c--;
					}
				}
				else
				{		// this has to be open for there to be an exit
					if(pathset==false)
					{
						pathset=true;
						yy=y+1;
						xx=x;
					}
				}
			}

			if(e==0)	//if dircetion is valid
			{
				if(c>1)	// if you dont have to go this way
				{	//there is a chance to set this as the path
					{
						int number = rand() %2 ;//0 or 1 again
						if(number==1)
						{
							pathset=true;
							yy=y;
							xx=x+1;
						}
						else
							c--;
					}
				}
				else
				{			// this has to be open for there to be an exit
					if(pathset==false)
					{
						pathset=true;
						yy=y;
						xx=x+1;
					}
				}
			}
		}





		//this->displaymap();
	}
}

void MapGen::setaround(int x,int y)
{


	ne=0;
	e=0;
	se=0;
	nw=0;
	sw=0;
	w=0;
	n=0;
	s=0;



	if(x-1<0)
	{
		nw=1;
		w=1;
		sw=1;
	}
	else if(x+1>=_w)
	{
		se=1;
		e=1;
		ne=1;
	}
	if(y-1<0)
	{
		nw=1;
		ne=1;
		n=1;
	}
	else if (y+1>=_h)
	{
		sw=1;
		se=1;
		s=1;
	}


	/*North east south west
	n	=	[y-1][x]
	ne	=	[y-1][x+1]
	e	=	[y]	 [x+1]
	se	=	[y+1][x+1]
	s	=	[y+1][x]
	sw	=	[y+1][x-1]
	w	=	[y]	 [x-1]
	nw	=	[y-1][x-1]
	*/


	if(n!=1)
		n=_map [y-1] [x];
	if(ne!=1)
		ne=_map[y-1] [x+1];
	if(e!=1)
		e=_map [y]  [x+1];
	if(se!=1)
		se=_map[y+1] [x+1];
	if(s!=1)
		s=_map [y+1] [x];
	if(sw!=1)
		sw=_map[y+1] [x-1];
	if(w!=1)
		w=_map [y]  [x-1];
	if(nw!=1)
		nw=_map[y-1] [x-1];

}

void MapGen::initialize(int x, int y)
{
	//makes space for 2d arrays
	_w = x;
	_h = y;
	_map = new int*[_h];
	for (int r = 0; r < _h; ++r)
	{
		_map[r] = new int[_w];
	}
	//fills 2d array with 3's (1=wall 0= open 3= not set)
	for (int r = 0; r < _h; ++r)
	{
		for (int c = 0; c < _w; ++c)
		{
			_map[r][c]=3;
		}
	}
	//this->displaymap();



}

void MapGen::branchGen(int odds,int loops)
{
	bool didsomething=true;
	while (didsomething==true)
	{
		didsomething= false;
		for(int r=0; r<_h;r++)
		{
			for(int c=0; c<_w;c++)
			{
				if(c-1>=0 && c+1<_w)
				{
					if((_map[r][c-1]==0 || _map[r][c+1]==0 ) && _map[r][c]==0 && (_map[r][c+1]==3 || _map[r][c-1]==3)) //open to the left or right
					{
						if(this->branch(c,r,odds,loops))
							didsomething=true;
					}
				
				}
				if(r-1>=0 && r+1<_w)
				{
					if((_map[r-1][c]==0 || _map[r+1][c]==0 ) && _map[r][c]==0 && (_map[r+1][c]==3 || _map[r-1][c]==3)) //open to the left or right
					{
						if(this->branch(c,r,odds,loops))
							didsomething=true;
					}	
				}
			}
		}
	}
}

bool MapGen::branch(int x, int y) //start x and start y
{

	int xx=0;	//temp x and y
	int yy=0;
	int changes=0;

	bool pathset = true;
	while(pathset==true)
	{
		pathset = false;
		//start gen
		int c=0;
		this->setaround(x,y);
		if(n==3)
			c++;
		if(w==3)
			c++;
		if(s==3)
			c++;
		if(e==3)
			c++;

		if(n==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %2 ;//0 or 1
				_map[y-1][x]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1 )
					{
						if(y-2>=0 && _map[y-2][x]==3)
						{
							pathset=true;
							yy=y-1;
							xx=x;
						}
						else
							c--;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y-1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y-1;
					xx=x;
				}
			}
		}



		if(w==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %2 ;//0 or 1
				_map[y][x-1]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x-1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x-1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x-1;
				}
			}
		}



		if(s==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %2 ;//0 or 1
				_map[y+1][x]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y+1;
						xx=x;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y+1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y+1;
					xx=x;
				}
			}
		}

		if(e==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %2 ;//0 or 1
				_map[y][x+1]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0 if jump hasent been taken
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x+1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x+1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x+1;
				}
			}
		}

		this->setaround(x,y);

		//no diagnal crap
		if(n==0 && w==0)
			_map[y-1][x-1]=1;
		if(n==0 && e==0)
			_map[y-1][x+1]=1;
		if(s==0 && w==0)
			_map[y+1][x-1]=1;
		if(s==0 && e==0)
			_map[y+1][x+1]=1;
	

		x=xx;	//no longer temp
		y=yy;
		if(pathset==true)
			changes++;
		//this->displaymap();
	}
	if(changes>0)
		return true;
	return false;
}

bool MapGen::branch(int x, int y, int odds) //start x and start y
{

	int xx=0;	//temp x and y
	int yy=0;
	int changes=0;

	bool pathset = true;
	while(pathset==true)
	{
		pathset = false;
		//start gen
		int c=0;
		this->setaround(x,y);
		if(n==3)
			c++;
		if(w==3)
			c++;
		if(s==3)
			c++;
		if(e==3)
			c++;

		if(n==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y-1][x]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1 )
					{
						if(y-2>=0 && _map[y-2][x]==3)
						{
							pathset=true;
							yy=y-1;
							xx=x;
						}
						else
							c--;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y-1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y-1;
					xx=x;
				}
			}
		}



		if(w==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y][x-1]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x-1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x-1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x-1;
				}
			}
		}



		if(s==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y+1][x]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y+1;
						xx=x;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y+1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y+1;
					xx=x;
				}
			}
		}

		if(e==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y][x+1]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0 if jump hasent been taken
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x+1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x+1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x+1;
				}
			}
		}

		this->setaround(x,y);

		//no diagnal crap
		if(n==0 && w==0)
			_map[y-1][x-1]=1;
		if(n==0 && e==0)
			_map[y-1][x+1]=1;
		if(s==0 && w==0)
			_map[y+1][x-1]=1;
		if(s==0 && e==0)
			_map[y+1][x+1]=1;
	
		x=xx;	//no longer temp
		y=yy;
		
		if(pathset==true)
			changes++;
		//this->displaymap();
	}
	if(changes>0)
		return true;
	return false;
}

bool MapGen::branch(int x, int y, int odds,int loop) //start x and start y
{

	int xx=0;	//temp x and y
	int yy=0;
	int changes=0;

	bool pathset = true;
	while(pathset==true)
	{
		pathset = false;
		//start gen
		int c=0;
		this->setaround(x,y);
		if(n==3)
			c++;
		if(w==3)
			c++;
		if(s==3)
			c++;
		if(e==3)
			c++;

		if(n==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y-1][x]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1 )
					{
						if(y-2>=0 && _map[y-2][x]==3)
						{
							pathset=true;
							yy=y-1;
							xx=x;
						}
						else
							c--;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y-1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y-1;
					xx=x;
				}
			}
		}



		if(w==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y][x-1]=number;
				if(number==1)			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x-1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x-1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x-1;
				}
			}
		}



		if(s==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y+1][x]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y+1;
						xx=x;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y+1][x]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y+1;
					xx=x;
				}
			}
		}

		if(e==3)	//if dircetion is valid
		{
			if(c>1)	// if you dont have to go this way
			{
				int number = rand() %odds ;//more odds for a 1
				if(number>1)
					number=1;
				_map[y][x+1]=number;
				if(number==1 )			//if it was a 1 lower the amout of chance for 0 if jump hasent been taken
				{
					c--;
				}
				else					//there is a chance to set this as the path
				{
					number = rand() %2 ;//0 or 1 again
					if(number==1)
					{
						pathset=true;
						yy=y;
						xx=x+1;
					}
					else
						c--;
				}
			}
			else
			{
				_map[y][x+1]=0	;		// this has to be open for there to be an exit
				if(pathset==false)
				{
					pathset=true;
					yy=y;
					xx=x+1;
				}
			}
		}

		this->setaround(x,y);

		//no diagnal crap
		if(n==0 && w==0)
			_map[y-1][x-1]=1;
		if(n==0 && e==0)
			_map[y-1][x+1]=1;
		if(s==0 && w==0)
			_map[y+1][x-1]=1;
		if(s==0 && e==0)
			_map[y+1][x+1]=1;
	

		x=xx;	//no longer temp
		y=yy;

		this->setaround(x,y);

		//no looping
		int loopc=0;
		if(n==0)
			loopc++;
		if(w==0)
			loopc++;
		if(s==0)
			loopc++;
		if(e==0)
			loopc++;
		if(loopc>1)
		{
			int temploop= rand() %loop; //how difficult is is to loop low loop means easy
			if(temploop!=0)
			{
				_map[y][x]=1;
				pathset=false;
			}
		}


		if(pathset==true)
			changes++;
		//this->displaymap();
	}
	if(changes>0)
		return true;
	return false;
}


void MapGen::displaymap()
{
	cout<<"  ";
	for(int cc= 0;cc< _w; cc++)
	{
		cout<<(cc % 10);
	}
	cout<<endl<<endl;
	for (int r = 0; r < _h; r++)
	{
		for (int c = -1; c < _w; c++)
		{
			if(c==-1)
				cout<<r % 10<<" ";
			else
				cout<<_map[r][c];
		}
		cout<<endl;
	}
	cout<<"\n\n\n"<<endl;
	return;
}

int MapGen::getW() const
{
	return _w;
}

int MapGen::getH() const
{
	return _h;
}

